package com.difsonapps.android.marker.beans;


import org.json.JSONException;
import org.json.JSONObject;

import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.util.MarkerUtil;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class User  implements Parcelable{
	protected int id;
	protected String firstname;
	protected String lastname;
	protected String middlename;
	protected String occupation;
	protected String aboutMe;
	protected String picture;
	protected String email;
	//TODO country, city, province, currency, local, sex
	public User() {
		super();
	}
	public User(Parcel in){
		this();
		this.id = in.readInt();
		this.firstname = in.readString();
		this.lastname = in.readString();
		this.middlename = in.readString();
		this.occupation = in.readString();
		this.aboutMe = in.readString();
		this.picture = in.readString();
		this.email = in.readString();
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getMiddlename() {
		return middlename;
	}
	public void setMiddlename(String middlename) {
		this.middlename = middlename;
	}
	public String getOccupation() {
		return occupation;
	}
	public void setOccupation(String occupation) {
		this.occupation = occupation;
	}
	public String getAboutMe() {
		return aboutMe;
	}
	public void setAboutMe(String aboutMe) {
		this.aboutMe = aboutMe;
	}
	

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	public String getPictureUrl(){
		if(picture==null) return "";
		return MarkerUtil.buildImageUrl(picture, AppConstant.IMAGES_FOLDER, null);
	}
	public String getName(){
		String result = "";
		if(lastname!=null&&!"".equals(lastname.trim())) result+=lastname.trim()+" ";
		if(middlename!=null&&!"".equals(middlename.trim())) result+=middlename.trim()+" ";
		if(firstname!=null&&!"".equals(firstname.trim())) result+=firstname.trim()+" ";
		return result.trim();
	}
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(firstname);
		dest.writeString(lastname);
		dest.writeString(middlename);
		dest.writeString(occupation);
		dest.writeString(aboutMe);
		dest.writeString(picture);
		dest.writeString(email);
	}	
	
	public static final Parcelable.Creator<User> CREATOR = new
		Parcelable.Creator<User>() {
			@Override
			public User createFromParcel(Parcel source) {
				return new User(source);
			}
			@Override
			public User[] newArray(int size) {
				return new User[size];
		}
	};
	
	public static User parseFromJsonObject(JSONObject json){
		if(json==null || !json.has("uid")) return null;
		User result = new User();
		try {
			result.setId(json.getInt("uid"));
			result.setLastname(json.getString("lastname"));
			result.setFirstname(json.getString("firstname"));
			result.setPicture(json.getString("photo"));
			result.setMiddlename(json.getString("middlename"));
			result.setOccupation(json.getString("occupation"));
			result.setAboutMe(json.getString("aboutme"));
			result.setEmail(json.getString("email"));
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("Parse User From JSON", e.getMessage());
			return null;
		}
		return result;
	}
	
}
